﻿using System;
using System.Collections.Generic;
using System.Text;
using FloatingWindowTools.FloatingMapbasicWindow;
using FloatingWindowTools.NormalFloatingWindow;
using System.Windows.Forms;
using System.Reflection;

namespace FloatingWindowTools
{
    public static class Entry
    {
        private static MapinfoWrapper mapinfo;
        private static MapinfoWrapper Mapinfo
        {
            get
            {
                if (mapinfo == null)
                {
                    mapinfo = MapinfoWrapper.CreateMapinfo(@"C:\Program Files\Mapinfo\Professional");
                }
                return mapinfo;
            }
        }

        private static Assembly Findmiadmdll()
        {
            var asms = AppDomain.CurrentDomain.GetAssemblies();

            foreach (var item in asms)
                if (item.GetName().Name == "miadm")
                    return item;

            return null;
        }

        /// <summary>
        /// Loads the miadm.dll assembly, we should call this in the Main of the mbx to stop the wrong instance of MapInfo being loaded.
        /// </summary>
        /// <remarks>Because we are loading the MapInfo miadm.dll on the fly, if to versions of MapInfo are open with the FloatingWindowTools program
        /// the first opened instance will have access to the wrong MapInfo instance.</remarks>
        public static void SetMapInfo(string path)
        {
            Assembly miadm = Findmiadmdll();
            if (miadm == null)
            {
                mapinfo = MapinfoWrapper.CreateMapinfo(path);
            }
            mapinfo = MapinfoWrapper.CreateMapinfo(miadm);
        }

        public static void FloatMapbasicWindow()
        {
            MapbasicWindowWrapper mapbasicwindow = new MapbasicWindowWrapper(Mapinfo, (int)Window.WindowType.WIN_MAPBASIC);

            if (!mapbasicwindow.IsOpen)
            {
                // Open the mapbasic window.
                Mapinfo.Do("Run Menu Command 608");
            }

            MapbasicWindowUI window = new MapbasicWindowUI(mapbasicwindow,Mapinfo);
            window.FormClosed += new FormClosedEventHandler(window_FormClosed);

            Window mapinfoWindow = new Window(Mapinfo, (int)Window.WindowType.WIN_MAPINFO);
            window.Show(mapinfoWindow);
        }

        static void window_FormClosed(object sender, FormClosedEventArgs e)
        {
            Mapinfo.Do("Alter Button ID 2005 Enable");

            //TODO: Enable menu item not working right.
            //Mapinfo.Do("Alter Menu Item Load Enable");
        }

        public static void FloatWindow()
        {
            string ID = Mapinfo.Eval("FrontWindow()");
            Window windowtofloat = new Window(Mapinfo, Convert.ToInt32(ID));

            if (windowtofloat.Title == "TempLcParent" || !windowtofloat.IsOpen)
            {
                MessageBox.Show("There is no selected window");
                return;
            }

            FloatingWindow window = new FloatingWindow(windowtofloat);

            Window mapinfoWindow = new Window(Mapinfo, (int)Window.WindowType.WIN_MAPINFO);

            window.Show(mapinfoWindow);
        }

        public static void ShowAbout()
        {
            AboutBox about = new AboutBox();
            about.ShowDialog();
        }
    }
}
